package com.xtayfjpk.zookeeper.test.master_workers.callback.master;

import org.apache.log4j.Logger;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.KeeperException.Code;

import com.xtayfjpk.zookeeper.test.master_workers.Master;
import com.xtayfjpk.zookeeper.test.master_workers.callback.BaseStringCallback;

public class CreateParrentCallback extends BaseStringCallback {
	private static final Logger LOGGER = Logger.getLogger(CreateParrentCallback.class);
	private Master master;
	
	public CreateParrentCallback(Master master) {
		this.master = master;
	}
	
	@Override
	protected void connectionLossCaseAction(int rc, String path, Object ctx, String name) {
		try {
			master.createParent(path, (byte[])ctx);
		} catch (KeeperException | InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	protected void okCaseAction(int rc, String path, Object ctx, String name) {
		LOGGER.info("Parent created");
	}
	
	@Override
	protected void nodeExistsCaseAction(int rc, String path, Object ctx,
			String name) {
		LOGGER.warn("Parent already registered:" + path);
	}
	
	@Override
	protected void defaultCaseAction(int rc, String path, Object ctx,
			String name) {
		LOGGER.error("Something went wrong:", KeeperException.create(Code.get(rc), path));
	}
}
